﻿using System;
using System.Xml;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace File_Logger
{
    class Program
    {
        public static string md5path;
        public static string logPath = "log.csv";
        static void Main(string[] args)
        {

            if (!File.Exists(logPath))
            {

                using (StreamWriter sw = File.CreateText(logPath))
                {
                }
            }

            Console.WriteLine("Logging Started");


            FileSystemWatcher m_Watcher = new System.IO.FileSystemWatcher();

            m_Watcher.Path = "c:\\Test\\";
            m_Watcher.IncludeSubdirectories = true;
            m_Watcher.Filter = "";
            m_Watcher.NotifyFilter = NotifyFilters.LastAccess |
                                     NotifyFilters.LastWrite |
                                     NotifyFilters.FileName |
                                     NotifyFilters.DirectoryName;

            // m_Watcher.Changed += new FileSystemEventHandler(OnChanged);

            //m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);
            //m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);
            m_Watcher.Created += new FileSystemEventHandler(onCreate);

            m_Watcher.EnableRaisingEvents = true;

            Console.WriteLine("Press \'q\' to quit.");
            while (Console.Read() != 'q') ;


        }

        private static void onCreate(object source, FileSystemEventArgs e)
        {
            md5path = e.FullPath;
            string xmlPath;
            bool md5match;

            if (Path.GetExtension(md5path) != ".xml")
            {
                xmlPath = Path.GetDirectoryName(md5path)+ "\\" + Path.GetFileNameWithoutExtension(md5path) + ".xml";
                Console.WriteLine(xmlPath);
                
                if(File.Exists(xmlPath))
                {
                    md5match = md5Parse(xmlPath);

                    if (md5match == true)
                    {
                        Console.WriteLine("MD5 match");
                        using (StreamWriter sw = File.AppendText(logPath))
                        {
                            sw.WriteLine(e.ChangeType + "," + e.FullPath + ",MD5 Match," + md5match);
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(logPath))
                        {
                            sw.WriteLine(e.ChangeType + "," + e.FullPath + ",MD5 Match," + md5match);
                        }
                    }

                }
                
            }
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {

            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);

            string path = "log.csv";

            if (!File.Exists(path))
            {

                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("FILE RENAMED" + ", " + e.OldFullPath + ", " + e.FullPath);
                }
            }

            using (StreamWriter sw = File.AppendText(path))
            {
                sw.WriteLine("FILE RENAMED" + ", " + e.OldFullPath + ", " + e.FullPath);
            }
        }

        static bool md5Parse(string path)
        {

            bool retVal;
            //Console.WriteLine(path + " md5Parse");

            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
            xmlDoc.Load(path); //* load the XML document from the specified file.

            //* Get elements.
            XmlNodeList md5 = xmlDoc.GetElementsByTagName("MD5Sum");

            retVal = md5Check(md5[0].InnerText);

                if (retVal == true)
                       {
                           return true;
                       }

                return false;

        }
       
        public static bool md5Check(string md5Source)
        {

            using (FileStream MD5Stream = new FileStream(md5path, FileMode.Open))
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(MD5Stream);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }

                if (md5Source == sb.ToString())
                {
                    return true;
                }
                else
                {
                    return false;

                }
            }
        }


    }
    //private static void OnChanged(object source, FileSystemEventArgs e)
    //{
    //    string md5path = e.FullPath;


    //    if (Path.GetExtension(md5path) == ".csv")
    //    {
    //        md5Parse(md5path);
    //        Console.WriteLine("File: " + e.FullPath + " am I just here " + e.ChangeType);
    //    }
    //   string path = "log.csv";

    //        if (!File.Exists(path))
    //        {

    //            using (StreamWriter sw = File.CreateText(path))
    //            {
    //                sw.WriteLine(e.ChangeType + ", " + e.FullPath); 
    //            }
    //        }

    //        using (StreamWriter sw = File.AppendText(path))
    //        {
    //            sw.WriteLine(e.ChangeType + ", " + e.FullPath); 
    //        }

    //}
}
